package de.fraunhofer.scai.lm.gui;


import java.util.Hashtable;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Dialog class showing the licence information.
 *
 * @version $LastChangedRevision$
 * @author Bea Hornef
 */
public class AccountDialog extends Dialog {

	Shell myShell;
	
	Button myOkButton;
	Text myAccountNameText;
	String myAccountName;
	String myAccountType;
	Hashtable<String, Account> myAccounts;
	Button myAcademicButton, myCommercialButton;
	/**
     * Constructor.
     * 
     * @param parentShell the parent shell.
     * @param accounts.
     */
    public AccountDialog(Shell parentShell, Hashtable<String, Account> accounts) {
    	super(parentShell);
    	
    	myAccountName = null;
    	myAccountType = null;
    	myAccounts    = accounts;
    }

    /**
     * {@inheritDoc}
     *
     * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
     */
    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setText("Create Account");
        myShell = newShell;
    }

    /**
     * {@inheritDoc}
     *
     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Control createDialogArea(Composite parent) {
        
        Composite composite = (Composite) super.createDialogArea(parent);

        GridLayout layout = new GridLayout(2, false);
        layout.marginTop = 10;
        layout.marginBottom = 10;
        layout.marginLeft = 10;
        layout.marginRight = 10;
        composite.setLayout(layout);

        final Label nameLabel = new Label(composite, SWT.NONE);
        nameLabel.setText("Please enter a valid account name:");

        myAccountNameText = new Text(composite, SWT.BORDER);
        myAccountNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
        myAccountNameText.setText("");
                
        final Label pleaseSelect = new Label(composite, SWT.NONE);
        pleaseSelect.setText("Please select a purpose of this account:");
                        
        myCommercialButton = new Button(composite, SWT.CHECK);
        myCommercialButton.setSelection(true);
        myCommercialButton.setText("Commercial");
        myCommercialButton.setSelection(false);
        myCommercialButton.addSelectionListener(new SelectionListener() {

            public void widgetSelected(SelectionEvent event) {
                myAcademicButton.setSelection(false);
            }
            public void widgetDefaultSelected(SelectionEvent event) {
                myAcademicButton.setSelection(false);
            }
          });

        new Label(composite, SWT.NONE);
                        
        myAcademicButton = new Button(composite, SWT.CHECK);
        myAcademicButton.setSelection(true);
        myAcademicButton.setText("Academic");
        myAcademicButton.setSelection(false);
        myAcademicButton.addSelectionListener(new SelectionListener() {

            public void widgetSelected(SelectionEvent event) {
            	myCommercialButton.setSelection(false);
            }
            public void widgetDefaultSelected(SelectionEvent event) {
            	myCommercialButton.setSelection(false);
            }
          });

        
        myAccountNameText.addListener(SWT.Modify, new Listener() {
            public void handleEvent(Event event) {
            }
          });

        return composite;
    }

	public String getAccountName() {
		return myAccountName;
	}
    
	public String getAccountType() {
		return myAccountType;
	}
    
	@Override
	protected void okPressed() {
		myAccountName = myAccountNameText.getText();
	    if (myAccountName.trim().length() == 0) {
	    	MessageDialog.openWarning(myShell, "Warning", "The account name is invalid, please try again!");
	    	return;
	    }
	    if (myAccounts.get(myAccountName) != null) {
			MessageDialog.openWarning(myShell, "Warning", "The account already exists, please try again!");
			return;
		}
	    
		if (! (myAcademicButton.getSelection() || myCommercialButton.getSelection())) {
	    	MessageDialog.openWarning(myShell, "Warning", "One purpose for this account has to be selected!");
			return;
		}
		
		if (myAcademicButton.getSelection()) {
			myAccountType = "nonCommercial";
		} else {
			myAccountType = "commercial";
		}
    	System.out.println("New account name = " + myAccountName);	            	
		super.okPressed();
	}

}